<?php

// @formatter:off
/**
 * @file switchgroup.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: switchgroup.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/switchgroup.php $
 */
// @formatter:on

class ActionControllerContentSwitchgroup extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_GROUP_SWITCH)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to jump to another group";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcProfile;

		$groups = array();
		foreach(WCGroupsHolder::I()->items() as $group) {
			if($wcProfile->grantedGroup($group) && is_readable($group->root)&&!$group->isHidden()) {
				$groups[$group->name] = $group;
			}
		}
		ksort($groups);

		$groupsAssigns = array();
		foreach($groups as $group) {
			$groupsAssigns[] = array(
				"id" => $group->id,
				"name" => $group->name
			);
		}
		$this->_manager->groups = $groupsAssigns;

		return $ok;
	}
}